﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Think.Lenovo.Com.DataAccess.NewsInfo;
using Think.Lenovo.Com.Models.NewsModel;

namespace Think.Lenovo.Com
{
    public partial class ViewNews : System.Web.UI.Page
    {
        NewsInfo info = new NewsInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBind_NewsCollection();
            }
        }

        protected void DataBind_NewsCollection()
        {
            GridView1.DataSource = info.GetAllNews();
            GridView1.DataBind();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            DataBind_NewsCollection();

        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            DataBind_NewsCollection();

        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            NewsModel model = new NewsModel();
            model = info.GetNewsInfo(Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString()));

            model.Id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            model.Taxis = Int32.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim());
            info.UpdateNews(model);
            GridView1.EditIndex = -1;
            DataBind_NewsCollection();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            DataBind_NewsCollection();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            info.DeleteNews(Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString()));
            DataBind_NewsCollection();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.Cells[7].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除：" + e.Row.Cells[1].Text + "吗?')");
                }
            }
        }
    }
}